﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using W.Warehouse.Api.Application.Command.AllocationCommand;
using W.Warehouse.Api.Application.Command.MoveCommand;
using W.Warehouse.Api.Application.Command.StorageCommand;
using W.Warehouse.Domain;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
using static WMSpublic.ParseAPI;

namespace W.Warehouse.Api.Controllers
{
    /// <summary>
    /// 移库管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class MoveController : ControllerBase
    {
        private readonly IMediator mediator;
        
        public MoveController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 添加移库信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddMove(MoveAddCommand command)
        {
            var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
            var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
            var name = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Name)?.Value;//获取名称
            command.UpdateName = name;
            command.user_id = Convert.ToInt32(uid);
            command.office_id = sid;
            return await mediator.Send(command);

        }
        /// <summary>
        /// 删除移库信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelectMove(MoveDelectCommand command)
        {
            
            return await mediator.Send(command);
        }
        /// <summary>
        /// 批量删除移库信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelectRangeMove(MoveDelectRangCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 修改移库
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateMove(MoveUpdateCommand command)
        {
            var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
            var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
            var name = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Name)?.Value;//获取名称
            command.UpdateName = name;
            command.user_id = Convert.ToInt32(uid);
            command.office_id = sid;
            return await mediator.Send(command);
        }
        /// <summary>
        /// 审核调拨管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<ApiResult<int>> MoveAsync(MoveAuditCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}
